package tree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @ClassName MaxDepth
 * @Description TODO
 * @Author lenovo
 * @Date 2023-03-09 19:13
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class MaxDepth {
    /**
     * @param root
     * @return
     */
    public int maxDepth(TreeNode root) {

        // root == 2 root.left == 1
        // root == 1 root.left == null 0 return 1

        //maxDepth1(3)==1
        if (root == null) return 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int num = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();

            while (size > 0) {
                TreeNode poll = queue.poll();

                if (poll.left != null) queue.add(poll.left);
                if (poll.right != null) queue.add(poll.right);

                size--;
            }
            num++;
        }
        return num;
    }


    public int maxDepth1(TreeNode root) {
        if (root == null) return 0;

        int left = maxDepth1(root.left);
        int right = maxDepth1(root.right);

        return Math.max(left, right) + 1;
    }

    public static void main(String[] args) {
        MaxDepth maxDepth = new MaxDepth();

        TreeNode treeNode = new TreeNode(2);

        TreeNode treeNodeLeft = new TreeNode(1);
        treeNode.left = treeNodeLeft;

        TreeNode treeNodeRight = new TreeNode(3);
        treeNodeLeft.left = treeNodeRight;
//        TreeNode treeNodeRight2 = new TreeNode(3);
//        treeNode.right = treeNodeRight2;
        System.out.println(maxDepth.maxDepth1(treeNode));
    }
}